<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\AjaxQuery;
use App\Models\Site;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class SiteController extends AdminController
{
    protected function title()
    {
        return '站点设置';
    }

    protected function grid()
    {
        $grid = new Grid(new Site);

        $grid->column('id', 'ID')->sortable();
        $grid->column('name', '参数名称');
        $grid->column('value', '参数值')->display(function ($value) {
            if ($this->type == 'image') {
                return "<img src='http://".env('QINIU_DOMAIN') . '/' . $this->value . "' width='100'>";
            } else {
                return $value;
            }
        });
        $grid->column('description', '描述');
        $grid->column('updated_at', '更新时间');

        $grid->disableRowSelector();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            if ($actions->row->disable_delete == 1) {
                $actions->disableDelete();
            }
        });
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new AjaxQuery(url(config('admin.route.prefix') . '/site/buildJsonFromSite'), '生成json文件'));
        });
        $grid->disableFilter();
        $grid->disablePagination();

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new Site);

        $param = request()->route()->parameters();
        $form->display('id', 'ID');
        $form->text('name', '参数名称');
        if (!empty($param)) {
            $data = Site::query()->find($param['config']);
            switch ($data->type) {
                case 'text':
                    $form->text('value', '参数值');
                    break;
                case 'textarea':
                    $form->textarea('value', '参数值');
                    break;
                case 'image':
                    $form->image('value', '参数值')->move($this->sitePath());
                    break;
                default:
                    $form->text('value', '参数值');
                    break;
            }
        } else {
            $form->text('value', '参数值');
        }

        $form->text('description', '描述');
        $form->select('type', '数据类型')->options(['text' => '文本','textarea' => '文本框', 'image' => '图片'])->default('text');
        $states = [
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
        ];
        $form->switch('disable_delete', '禁止删除')->states($states);

        return $form;
    }

    /**
     * 生成站点json配置文件
     * @return \Illuminate\Http\JsonResponse
     */
    public function buildJsonFromSite()
    {
        $data = Site::query()->pluck('value', 'name')->toJson(JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
        $sites = storage_path('/json/sites.json');
        $result = file_put_contents($sites, $data);
        return success('生成json文件成功');
    }
}
